package cn.com.shopec.erp.product.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.shopec.erp.common.constant.ProductSCMConstant;
import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.common.utils.DateUtil;
import cn.com.shopec.erp.common.utils.LogicUtil;
import cn.com.shopec.erp.common.utils.NoUtil;
import cn.com.shopec.erp.product.commo.DefectReplceVoConvertor;
import cn.com.shopec.erp.product.commo.DefectiveVoConvertor;
import cn.com.shopec.erp.product.dao.IDefectReplceDao;
import cn.com.shopec.erp.product.dao.IDefectiveDao;
import cn.com.shopec.erp.product.entity.DefectReplce;
import cn.com.shopec.erp.product.entity.Defective;
import cn.com.shopec.erp.product.entity.Product;
import cn.com.shopec.erp.product.service.IDefectReplceService;
import cn.com.shopec.erp.product.service.IDefectiveService;
import cn.com.shopec.erp.product.service.IProductService;
import cn.com.shopec.erp.product.vo.DefectReplceVo;
import cn.com.shopec.erp.product.vo.DefectiveVo;
import cn.com.shopec.erp.purchase.dao.IStockPurchaseDao;
import cn.com.shopec.erp.purchase.service.IStockPurchaseService;
import cn.com.shopec.erp.system.common.UserUtils;

/**
 * @ClassName: IProdDefectiveImpl
 * @Description: 次品的Service层的接口实现
 * @author: LiangShunWang
 * @date: 2016-11-21 下午16:40:13
 */
@Service
public class DefectReplceServiceImpl implements IDefectReplceService {

	@Resource
	private DefectReplceVoConvertor defectReplceVoConvertor;

	@Resource
	private IDefectReplceDao defectReplceDao;

	@Resource
	private IProductService productService;

	@Resource
	private IStockPurchaseService stockPurchaseService;

	@Resource
	private IDefectiveService idefectiveService;

	@Resource
	private IDefectiveDao defectiveDao;

	@Resource
	private DefectiveVoConvertor defectiveVoConvertor;

	@Resource
	private IStockPurchaseDao stockPurchaseDao;

	/**
	 * 
	 * @Title: getPagedBrandList
	 * @Description: 分页查询次品信息
	 * @param brandVo
	 * @param pageQuery
	 * @return
	 * @throws @author:
	 *             LiangShunWang
	 * @date: 2016-11-21下午16:52:05
	 */
	public PageFinder<DefectReplceVo> getPagedBrandList(DefectReplceVo defectReplceVo, PageQuery pageQuery)
			throws Exception {
		DetachedCriteria dc = getDetachedCriteria(defectReplceVo);
		dc.addOrder(Order.desc("createTime"));
		PageFinder<DefectReplce> pagedBrandList = this.defectReplceDao.pagedByCriteria(dc, pageQuery.getPage(),
				pageQuery.getPageSize());
		return this.defectReplceVoConvertor.convert2PagedDefectiveVos(pagedBrandList);
	}
	
	DetachedCriteria getDetachedCriteria(DefectReplceVo defectReplceVo) {
		DetachedCriteria dc = this.defectReplceDao.createCriteria();
		if (defectReplceVo != null) {
			// 退货编号
			if (LogicUtil.isNotNullAndEmpty(defectReplceVo.getDefectReplceId())) {
				dc.add(Restrictions.eq("defectReplceId", defectReplceVo.getDefectReplceId()));
			}
			// 处理状态
			if (LogicUtil.isNotNullAndEmpty(defectReplceVo.getProcessState()) && (!defectReplceVo.getProcessState().equals("0"))) {
				dc.add(Restrictions.eq("processState", defectReplceVo.getProcessState().trim()));
			}

			// 业务类型
			if (LogicUtil.isNotNullAndEmpty(defectReplceVo.getReplceType())
					&& (!defectReplceVo.getReplceType().equals("0"))) {
				// dc.add(Restrictions.like("warehouse", "%" +
				// defectiveVo.getWarehouse().trim() + "%"));
				dc.add(Restrictions.eq("replceType", defectReplceVo.getReplceType()));
			}
			dc.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_YES));
		}
		return dc;
	}
	
	/**
	 * 
	 * @Title: addDefective
	 * @Description: 增加次品退换
	 * @param defectiveVo
	 * @return
	 * @throws @author:
	 *             LiangShunWang
	 * @date: 2016-11-22下午11:52:05
	 */
	@Override
	@Transactional
	public void addDefectReplce(List<DefectReplce> defectiveVo, DefectReplceVo defectReplceVo) throws Exception {
		String id = null;
		if (defectiveVo != null) {
			for (DefectReplce defective : defectiveVo) {
				if (defective.getId() != null) {
					defective.setId(id);
					defective.setDefectReplceId(NoUtil.getNo("DE"));
					defective.setCreateTime(DateUtil.formatTimesTampDate(new Date()));
					defective.setDefectReplceName(defectReplceVo.getDefectReplceName());
					defective.setSupCode(defectReplceVo.getSupCode());
					defective.setSupName(defectReplceVo.getSupName());
					defective.setReplceType(defectReplceVo.getReplceType());
					defective.setSupCode(defectReplceVo.getSupCode());
					defective.setSupName(defectReplceVo.getSupName());
					defective.setInitiater(UserUtils.getLoginUserName());
					defective.setInitiateTime(DateUtil.formatTimesTampDate(new Date()));
					defective.setReplceType(defectReplceVo.getReplceType());

					
					defective.setProcessState(ProductSCMConstant.DEFECTREPLCE_STATE_0);
					DefectiveVo defect = idefectiveService.getDefective(defective.getDeNo());
					Defective defec = defectiveVoConvertor.convert2Defective(defect);
					defec.setStorageState(ProductSCMConstant.STORAGE_STATE_DEFECTRELCE);
					// 2.修改次品列表的处理状态
					defectiveDao.update(defec);
					// 3.修改采购单和StockPurchase表的是否生成次品退换货单为1
					/*
					 * stockPurchase.setDefectiveReturnState(ProductSCMConstant.
					 * SOCK_PURCHSE_STATUS);
					 * stockPurchaseDao.update(stockPurchase);
					 */
					// 获取货品所对应的规格，和条码
					Product product = this.productService.getProductIntfoByCommon(defective.getCommoNo());
					defective.setProdBarCode(product.getProdBarCode());
					defective.setSpecification(product.getSpecification());
					defectReplceDao.save(defective);
				}
			}
		}
	}

	/**
	 * 
	 * @Title: getPurchaseOrder
	 * @Description: 根据次品退货单号查询次品信息
	 * @param defectiveVo
	 * @return
	 * @throws @author:
	 *             LiangShunWang
	 * @date: 2016-11-22下午11:52:05
	 */
	@Override
	public DefectReplceVo getPurchaseOrder(String DefectReplceId) {
		return this.defectReplceVoConvertor
				.convert2DefectiveVo(this.defectReplceDao.get("defectReplceId", DefectReplceId));
	}

	/**
	 * @Title: updatePoProductPriceAndQuantity
	 * @Description:根据采购单号查询采购单名称
	 * @param poNo
	 * @throws Exception
	 * @throws @author:
	 *             Liang Shun Wang
	 * @date: 2013-5-24下午9:35:26
	 */
	@Override
	public PageFinder<DefectReplceVo> getDefectReplceById(String defectReplceId, PageQuery pageQuery) throws Exception {
		DetachedCriteria criteria = this.defectReplceDao.createCriteria();

		if (defectReplceId != null) {
			// criteria.add(Restrictions.ilike("defectReplceId",
			// defectReplceId.trim(),MatchMode.ANYWHERE));

			if (LogicUtil.isNotNullAndEmpty(defectReplceId)) {
				criteria.add(Restrictions.eq("defectReplceId", defectReplceId));
			}
		}
		criteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_YES)); // 未删除
		PageFinder<DefectReplce> pagedBrandList = this.defectReplceDao.pagedByCriteria(criteria, pageQuery.getPage(),
				pageQuery.getPageSize());
		return this.defectReplceVoConvertor.convert2PagedDefectiveVos(pagedBrandList);
		//
	}

	/**
	 * 
	 * @Title: updateAuditResultPurchaseOrder
	 * @Description: 修改采购单退换货状态
	 * @param brandVo
	 * @return
	 * @throws @author:
	 *             Liang Shun Wang
	 * @date: 2013-5-26下午04:17:40
	 */
	@Override
	public void updateAuditResultDefectReplce(String defectReplceId, String status) throws Exception {
		if (LogicUtil.isNotNull(defectReplceId)) {
			DefectReplce defect = this.defectReplceDao.get("defectReplceId", defectReplceId);
			this.defectReplceDao.update(
					this.defectReplceVoConvertor.convert2DefectiveUpdateAuditResult(defectReplceId, defect, status));
		}
	}

	/**
	 * 作废次品退货信息
	 * @throws Exception 
	 */
	@Override
	@Transactional
	public boolean delDefectiveReplce(String defectiveReplceNo) throws Exception {
		Boolean  bool = false;
		if(!defectiveReplceNo.equals("") && defectiveReplceNo!=null){
			DefectReplce defect = this.defectReplceDao.get("defectReplceId", defectiveReplceNo);
			if(LogicUtil.isNotNull(defect)){
				defect.setDeleteFlag(SZSCMConstant.DELETE_FLAG_NO);
				this.defectReplceDao.update(defect);
				bool = true;
			}else{
				return bool;
			}
		}else{
			return bool;
		}
		return bool;
	}
	

}